﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CrawlerCore;
using HtmlAgilityPack;
using WapCrawlerLib;

namespace WapCrawlerCore.Classes
{
    public class Xtgem
    {
        private BrowserSession_General br = new BrowserSession_General();
        private System.Net.Cookie session;
        public void Login(string username, string password)
        {
            br.Get("http://xtgem.com");
            session = br.Response.Cookies["session"];

            System.Net.CookieCollection cookieCollect = new System.Net.CookieCollection();
            cookieCollect.Add(session);
            br.SaveCookiesFrom(cookieCollect);
            br.PostParams = string.Format("app=1&user={0}&subdomain=xtgem.com&pass={1}&submit=Login", username, password);
            br.Post(string.Format("http://xtgem.com/user/login/submit?sessid={0}&__ajax=1 ", session));
            session = br.Response.Cookies["session"];
        }

        public void CreateFolder(string folder, string name)
        {
            //string folder = "/Games/VietHoa";
            //string name = "2";
            string arg1 = string.Format("dir={0}", folder,name);
            string arg2 = string.Format("filename={1}", folder, name);

            string url = string.Format(@"http://xtgem.com/api/act?create_folder&{0}&{1}&__u=mobiwapvn.xtgem.com", HttpUtility.UrlEncode(arg1), HttpUtility.UrlEncode(arg2));
            
            System.Net.CookieCollection cookieCollect = new System.Net.CookieCollection();
            cookieCollect.Add(session);
            br.SaveCookiesFrom(cookieCollect);
            string args = HttpUtility.UrlEncode(string.Format("{0};{1}", arg1, arg2));
            br.PostParams = string.Format(@"action=create_folder&version=&args={0}", args);
            br.Post(url);

        }

        public void CreateFile()
        {
            
        }

        public void UploadFile()
        {
            
        }
    }
}